<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="专业" prop="specId">
        <el-select v-model="queryParams.specId" clearable placeholder="请选择专业">
            <el-option
              v-for="item in specialtyList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              size="small">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idcard">
        <el-input
          v-model="queryParams.idcard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报到日期">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['student:studentCheckin:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentCheckinList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="专业" align="center" prop="spec.title" />
      <el-table-column label="姓名" align="center" prop="apply.name" />
      <el-table-column label="身份证号" align="center" prop="apply.idcard" />
      <el-table-column label="班级" align="center" prop="clazz.title" />
      <el-table-column label="是否住校" align="center" prop="apply.isLodge" :formatter="isLodgeFormat" />
      <el-table-column label="宿舍" align="center" prop="room.title" />
      <el-table-column label="缴费" align="center" prop="pays" />
      <el-table-column label="报到日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['student:studentCheckin:edit']"
          >报到</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改招生报到对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="专业" prop="specId">
          <el-select disabled v-model="form.specId" clearable placeholder="请选择专业">
              <el-option
                v-for="item in specialtyList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
                size="small">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" disabled />
        </el-form-item>
        <el-form-item label="身份证号" prop="idcard">
          <el-input v-model="form.idcard" disabled/>
        </el-form-item>
        <el-form-item label="班级" prop="classId">
          <el-select v-model="form.classId" clearable placeholder="请选择班级">
              <el-option
                v-for="item in clazzList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
                size="small">
              </el-option>
            </el-select>
        </el-form-item>
      <el-form-item label="是否住校" prop="isLodge">
        <el-select v-model="form.isLodge" placeholder="请选择是否住校" clearable size="small">
          <el-option
            v-for="dict in yesnoOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
        <el-form-item label="宿舍" prop="roomId" v-if="form.isLodge == 'Y' ">
          <el-select clearable v-model="form.roomId" filterable placeholder="请选择房间">
            <el-option
              v-for="item in roomList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="缴费" prop="pays">
          <el-input-number :precision="2" :step="100" v-model="form.pays" placeholder="请输入缴费" />
        </el-form-item>
        <el-form-item label="备注说明" prop="comment">
          <el-input v-model="form.comment" placeholder="请输入备注说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStudentCheckin, getStudentCheckin, delStudentCheckin, addStudentCheckin, updateStudentCheckin, exportStudentCheckin } from "@/api/student/studentCheckin";
import { getStudentApply } from "@/api/student/studentApply";
import { selectSpecialty} from "@/api/base/specialty";
import { selectClazz} from "@/api/base/clazz";
import { selectRoom } from "@/api/base/room";

export default {
  name: "StudentCheckin",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专业表格数据
      specialtyList: [],
      // 是否字典
      yesnoOptions: [],
      // 房间数据
      roomList: [],
      // 班级表格数据
      clazzList: [],
      // 招生报到表格数据
      studentCheckinList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        signupId: null,
        classId: null,
        comment: null
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classId: [
          { required: true, message: "班级不能为空", trigger: "blur" }
        ],
        pays: [
          { required: true, message: "缴费不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_yes_no").then(response => {
      this.yesnoOptions = response.data;
    });
    selectSpecialty({'status':1}).then(response => {
      this.specialtyList = response.rows;
    });
    selectRoom({status:1,type:'3'}).then(response => {
      this.roomList = response.rows;
    });
  },
  methods: {
    /** 查询招生报到列表 */
    getList() {
      this.loading = true;
      listStudentCheckin(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.studentCheckinList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否住校字典翻译
    isLodgeFormat(row, column) {
      return this.selectDictLabel(this.yesnoOptions, row.apply.isLodge);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applyId: null,
        classId: null,
        isLodge: null,
        roomId: null,
        pays: null,
        comment: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "招生报到";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      if(row.id){
        getStudentCheckin(row.id).then(response => {
          selectClazz({status:1, specId:response.data.specId}).then(response => {
            this.clazzList = response.rows;
          });

          this.form = response.data;
          this.form.name = response.data.apply.name;
          this.form.idcard = response.data.apply.idcard;
          this.form.specId = response.data.apply.specId;
          this.form.isLodge = response.data.roomId == null ? 'N' : 'Y';
          this.open = true;
          this.title = "招生报到";
        });
      }else{
        getStudentApply(row.apply.id).then(response => {
          selectClazz({status:1, specId:response.data.specId}).then(response => {
            this.clazzList = response.rows;
          });

          this.form.name = response.data.name;
          this.form.applyId = response.data.id;
          this.form.idcard = response.data.idcard;
          this.form.specId = response.data.specId;
          this.form.isLodge = response.data.isLodge;
          this.form.roomId = response.data.roomId;
          this.open = true;
          this.title = "招生报到";
        });
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudentCheckin(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudentCheckin(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除招生报到编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStudentCheckin(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有招生报到数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStudentCheckin(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
